<?php
/*****************************************************
 * File name: HeroModel.php
 * Create date: 2019/03/28
 * Author: liuyue
 * Description: 英雄数据库接口
 *****************************************************/


namespace Model;

use Lib;

class HeroModel
{

        private $db = null;
        private $table = 'tbhero';


        const SHAPE_ARRAY =  [1 => '稀有', 2 => '史诗', 3 => '传说'];
        const QUALITY_ARRAY = [
            1 => '绿色一星', 2 => '绿色二星', 3 => '绿色三星',
            4 => '蓝色一星', 5 => '蓝色二星', 6 => '蓝色三星',
            7 => '紫色一星', 8 => '紫色二星', 9 => '紫色三星',
        ];
        public function __construct()
        {
            $this->db = Lib\Db\DbModel::init(); // 构造方法，线上自动切换
        }

        /**
         * 根据英雄Id以及版本Id获取英雄信息
         * @param $heroId
         * @param $versionId
         * @return array
         */
        public function getHeroInfo($heroId,$where)
        {
            //条件
            $where['heroId'] = $heroId;
            $hero = $this->db->table('tbhero')->where($where)->find();
            return $hero;
        }

        public function getFormatHero($where)
        {

            $data = $this->db->table('tbhero')->where($where)->order("heroId","asc")->select('heroId,name');
            $res = [];
            foreach ($data as $item) {
                $res[$item['heroId']] = $item['name'];
            }
            return $res;
        }
        //
        public function updateHero($heroId,$where,$data)
        {
            $where['heroId'] = $heroId;
            $res = $this->db->table('tbhero')->where($where)->update($data);

            return $res;
        }


        /**
         * 判断是否存在英文名称,英雄中文名称,英雄称号
         * @param $heroId
         * @param $alias
         * @param $versionNum
         * @param $type : arial,name,title
         * @return bool
         */
        public function isExistHeroParameter($heroId,$alias,$versionNum,$type)
        {
            $where= [
                $type => $alias,
               'versionId' => $versionNum,
               'isDelete' => 0,
               'heroId' => ['!=', $heroId]
            ];

            $res = $this->db->table('tbhero')->where($where)->find($type);
            $isIn = in_array($alias,$res);
            if($isIn){
               return true;
            }else{
               return false;
            }
        }

    public function insertHeroOne($versionId,$seasonId,$data)
    {
        $arr_keys = array_keys($data);
        array_push($arr_keys,'versionId');
        array_push($arr_keys,'seasonId');
        $sql = 'INSERT INTO '.$this->table .' (' . implode(',' ,$arr_keys) . ') value';
        $data['versionId'] = $versionId;
        $data['seasonId'] = $seasonId;
        $arr_values = array_values($data);
        $sql.= " ('" . implode("','" ,$arr_values) . "'),";
        $sql = substr($sql ,0 ,-1);
        $result = $this->db->table($this->table)->exec($sql);
        return $result;
    }
}